angular.module('moti.controllers.spotscreat',['directiveModule', 'moti.services.spotsService', 'moti.directive.modal', 'moti.directive.uploaderImg', 'moti.directive.uploaderAudio'])
    .controller('spotsCreatCtrl', ['$scope', '$location', '$routeParams', 'SpotsInterface', '$modal', '$sce', function($scope, $location, $routeParams, SpotsInterface, $modal, $sce){

        //判断创建or修改
        $scope.edit = !!$routeParams.spotId;

        var editor = null;
        //修改页面情况获取当前景点接口
        if($scope.edit){
            SpotsInterface.getSpot($routeParams.regionId, $routeParams.spotId).then(function(data){
                $scope.spot = data;
                //audio标签url设置信任
                $scope.sce = $sce.trustAsResourceUrl;
                //创建ckeditor，配置word粘贴时去除格式
                editor = CKEDITOR.replace("editor");
                editor.editorConfig = function(config){
                    config.pasteFromWordRemoveFontStyles = false;
                    config.pasteFromWordRemoveStyles = false;
                };
                setTimeout(function(){
                    editor.setData(data.content);
                }, 500);
            });
        }else{
            editor = CKEDITOR.replace("editor");
            editor.editorConfig = function(config){
                config.pasteFromWordRemoveFontStyles = false;
                config.pasteFromWordRemoveStyles = false;
            };
        }

        //删除录音
        $scope.deleteAudio = function(){
            $scope.spot.audio = $scope.spot.audio_id = null;
        };

        //提交
        $scope.spotsCreatForm = function(){
            if($scope.spots_creat_form.$valid){
                $scope.spot.content = editor.getData();

                if($scope.edit){
                    SpotsInterface.putSpot($routeParams.regionId, $routeParams.spotId, $scope.spot).then(function(data){
                        $modal({id : 'tip-modal', text : "修改景点成功"}).show().autoHide();
                        $location.path('/regionsDetail/' + $routeParams.regionId);
                    });
                }else{
                    SpotsInterface.postSpot($routeParams.regionId, $scope.spot).then(function(data){
                        $modal({id : 'tip-modal', text : "创建景点成功"}).show().autoHide();
                        $location.path('/regionsDetail/' + $routeParams.regionId);
                    });
                }
            }
        }
    }]);